package com.sskj.b70.v.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;

import com.bulong.rudeness.RudenessScreenHelper;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.sskj.b70.MyAppLication;
import com.sskj.b70.R;
import com.sskj.b70.common.App;
import com.sskj.b70.common.adapter.BaseAdapter;
import com.sskj.b70.common.adapter.ViewHolder;
import com.sskj.b70.common.decoration.DividerLineItemDecoration;
import com.sskj.b70.m.bean.RechargeListBean;
import com.sskj.b70.p.fragment.CapitalDetailsPresenter;
import com.sskj.b70.v.activity.CapitalDetailsActivity;
import com.sskj.b70.v.base.BaseFragment;

import java.util.List;

import butterknife.BindView;

/**
 * ProjectName：new_bfex
 * DESC: (资金管理-->资金明细)
 * Created by 李岩 on 2018/7/11 0011
 * updateName:(修改人名称)
 * updateTime:(修改时间)
 * updateDesc:(修改内容)
 */
public class CapitalDetailsFragment extends BaseFragment<CapitalDetailsActivity, CapitalDetailsPresenter> {
    @BindView(R.id.recyclerView)
    RecyclerView mRecyclerView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout mRefreshLayout;
    private BaseAdapter<RechargeListBean> mAdapter;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_recyclerview;
    }

    @Override
    public CapitalDetailsPresenter getPresenter() {
        return new CapitalDetailsPresenter();
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        int index = getArguments().getInt("index", -1);
        int assetId = getArguments().getInt("assetId", -1);
        String stockCode = getArguments().getString("stockCode");
        mRefreshLayout.setRefreshHeader(new MaterialHeader(mActivity));

        mRefreshLayout.setOnRefreshListener(loadmorelistener -> {
            mRefreshLayout.finishRefresh();
            switch (index) {
                case 0:  // 充币
                    mPresenter.getRechargeList(MyAppLication.getID(), stockCode, "1", true);
                    break;
                case 1:  // 提币
//                    mPresenter.getWithdrawList(assetId, true);
                    mPresenter.getRechargeList(MyAppLication.getID(), stockCode, "2", true);
                    break;
                case 2:  // 其他
                    if (assetId == 0) {
                        mPresenter.getBalanceRecordList(true);
                    } else {
                        mPresenter.getAssetRecordList(MyAppLication.getID(), stockCode, true);
                    }
                    break;
            }
        });
        mRefreshLayout.setEnableLoadMore(false);
        mRefreshLayout.setOnLoadMoreListener(loadmorelistener -> {
            mRefreshLayout.finishLoadMore();
            switch (index) {
                case 0:
                    mPresenter.getRechargeList(MyAppLication.getID(), stockCode, "1", false);
                    break;
                case 1:
                    mPresenter.getRechargeList(MyAppLication.getID(), stockCode, "2", false);
                    break;
                case 2:
//                    if (assetId == 0) {
//                        mPresenter.getBalanceRecordList(false);
//                    } else {
//                        mPresenter.getAssetRecordList(assetId, stockCode, false);
//                    }
                    mPresenter.getAssetRecordList(MyAppLication.getID(), stockCode, false);
                    break;
            }
        });
        if (index == 0) {
            mRefreshLayout.autoRefresh();
        }
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mActivity));
//        mRecyclerView.addItemDecoration(new DividerItemDecoration(mActivity, LinearLayoutManager.VERTICAL));
        mRecyclerView.addItemDecoration(new DividerLineItemDecoration(getActivity())
                .setLastDraw(false)
                .setPaintWidth((int) RudenessScreenHelper.pt2px(getActivity(), 2))
                .setDividerColor(Color.parseColor("#f5f5f5")));
        if (index == 2) {
            mAdapter = new BaseAdapter<RechargeListBean>(R.layout.item_capital_details_asset, null, mRecyclerView) {

                @Override
                public void bind(ViewHolder holder, RechargeListBean item) {
                    holder.setText(R.id.item_capital_asset_count, item.getPrice());  // 价钱
//                    holder.setText(R.id.item_capital_asset_time, DateUtil.getDateFormat(item.getAddtime() * 1000, DateUtil.PATTERN_K));  //时间
                    holder.setText(R.id.item_capital_asset_time, item.getCreateTime());
                    holder.setText(R.id.item_capital_asset_remark, TextUtils.isEmpty(item.getMemo()) ? item.getType() : item.getMemo());  //备注
                }
            };
        } else {
            mAdapter = new BaseAdapter<RechargeListBean>(R.layout.item_capital_details, null, mRecyclerView) {

                @Override
                public void bind(ViewHolder holder, RechargeListBean item) {

                    holder.setTextColor(R.id.item_capotal_details_status, getResources().getColor(item.getState() == 3 ? R.color.red_eb : R.color.textColorNormal));
                    switch (index) {
                        case 0:
                            holder.setText(R.id.item_capotal_details_withdraw_address_key, "充币地址\t");
                            holder.setText(R.id.item_capotal_details_withdraw_address, item.getWalletAddr());
                            holder.setText(R.id.item_capotal_details_submit_count_key, "充币数量\t");
                            holder.setText(R.id.item_capotal_details_status, item.getState() == 3 ? App.INSTANCE.getString(R.string.strCapitalDetailsFragment10) : item.getState() == 2 ? App.INSTANCE.getString(R.string.strCapitalDetailsFragment11) : item.getState() == 1 ? App.INSTANCE.getString(R.string.strCapitalDetailsFragment12) : App.INSTANCE.getString(R.string.strCapitalDetailsFragment10));
                            break;
                        case 1:
                            holder.setText(R.id.item_capotal_details_withdraw_address_key, "提币地址\t");
                            holder.setText(R.id.item_capotal_details_withdraw_address, item.getWalletAddr());
                            holder.setText(R.id.item_capotal_details_submit_count_key, "提币数量\t");
                            holder.setText(R.id.item_capotal_details_status, item.getState() == 3 ? App.INSTANCE.getString(R.string.strCapitalDetailsFragment10) : item.getState() == 2 ? App.INSTANCE.getString(R.string.strCapitalDetailsFragment11) : item.getState() == 1 ? App.INSTANCE.getString(R.string.strCapitalDetailsFragment12) : App.INSTANCE.getString(R.string.strCapitalDetailsFragment10));
                            break;
                    }
                    if (item.getState() == 3) {
                        holder.setText(R.id.item_capotal_details_err_hint, item.getMemo());
                        holder.setGone(R.id.item_capotal_details_err_hint, true);
                    } else {
                        holder.setGone(R.id.item_capotal_details_err_hint, false);
                    }
                    holder.setText(R.id.item_capotal_details_submit_count, item.getPrice());
                    holder.setText(R.id.item_capotal_details_start_time, item.getCreateTime());
                    holder.setText(R.id.item_capotal_details_end_time, item.getCheck_time());

                }
            };
        }

//        View inflate = LayoutInflater.from(mActivity).inflate(R.layout.empty_view, null);
//        mAdapter.addHeaderView(inflate);
        mAdapter.setEmtyView();
    }

    public void onRechargeRecordSuccess(List<RechargeListBean> data, boolean isRefresh) {
        if (isRefresh) {
            mAdapter.setNewData(data);
        } else {
            mAdapter.addData(data);
        }
    }

    public void onWithdrawRecordSuccess(List<RechargeListBean> data, boolean isRefresh) {
        if (isRefresh) {
            mAdapter.setNewData(data);
        } else {
            mAdapter.addData(data);
        }
    }

    public void onAssetRecordSuccess(List<RechargeListBean> res, boolean isRefresh) {
        if (isRefresh) {
            mAdapter.setNewData(res);
        } else {
            mAdapter.addData(res);
        }
    }

    public void update() {
        if (mAdapter.getData().size() <= 0) {
            mRefreshLayout.autoRefresh();
        }
    }
}
